<?php

namespace Apps\AppApi\Method\User;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\CosOnlineAppointment;
use Apps\Common\Models\BeautyParlor;
use Apps\Common\Models\BeautyParlorAttr;
use Apps\Common\Models\BeautyParlorService;
use Apps\Common\Libs\PicUrl;

/**
 * 我的预约
 */
class MyOrder extends BaseMethod
{
    public function run()
    {
        /**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }

        $page = intval($this->getDataItem('page', 1));
        $limit = $this->getConfig()->limit;

        $offset = ($page - 1) * $limit;

        $user_id = $this->getUserAuth()->userId;

        $list = CosOnlineAppointment::query()->columns([
            'id',
            'set_man_id',
            'shop_id',
            'shop_name',
            'online_product_id',
            'product_name',
            'appointment_time',
            'appointment_state',
            'set_time',
            'type'
        ])->where('appointment_state in(0, 1) and set_man_id = ' . $user_id)->orderBy('appointment_time DESC')->limit($limit, $offset)->execute()->toArray();

        $result = [];
        foreach ($list as $val) {
            $cover = '';
            $price = ''; // 初始单价
            if ($val['type'] == 1) { // 美容院
                if ($BeautyParlorAttr = BeautyParlorAttr::findFirst('beauty_parlor_id = ' . $val['shop_id'])) {
                    $price = $BeautyParlorAttr->low_price . '元';
                }
                if ($BeautyParlor = BeautyParlor::findFirst('bp_id = ' . $val['shop_id'])) {
                    $cover = $BeautyParlor->bp_cover;
                }
                $result[] = [
                    'id' => $val['id'],
                    'order_id' => $val['shop_id'],
                    'title' => $val['shop_name'],
                    'cover' => $cover ? PicUrl::BeautyParlorCover($cover, $this->getDi()) : '',
                    'price' => $price,
                    'time' => $val['appointment_time'],
                    'add_time' => $val['set_time'],
                    'status' => $val['appointment_state'],
                    'type' => '1'
                ];
            } elseif ($val['type'] == '2') {
                if ($BeautyParlorService = BeautyParlorService::findFirst('service_id = ' . $val['online_product_id'])) {

                    $price = $BeautyParlorService->service_price. '元' . ($BeautyParlorService->service_unit ? '/' . $BeautyParlorService->service_unit : '');
                    $cover = $BeautyParlorService->service_cover;
                }
                $result[] = [
                    'id' => $val['id'],
                    'order_id' => $val['online_product_id'],
                    'title' => $val['product_name'] ?: '',
                    'cover' => $cover ? PicUrl::BeautyParlorCover($cover, $this->getDi()) : '',
                    'price' => $price,
                    'time' => $val['appointment_time'],
                    'add_time' => $val['set_time'],
                    'status' => $val['appointment_state'],
                    'type' => '2'
                ];
            } else {
                continue;
            }
        }

        $this->setResult($result);
    }
}